/**
 * 
 */
package com.jm.web.builder;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.form.Button;
import com.jm.web.form.Toolbar;
import com.jm.web.tags.PageTitleTag;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 *
 * 2011/9/29 上午8:56:01
 */
public class PageTitleBuilder extends BaseBuilder{
	PageTitleTag pageTitleTag = null ;
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		pageTitleTag = (PageTitleTag)this.getBaseTag();
	}
	
	@Override
	public void RenderEnd() {
		if(!this.pageTitleTag.getPageTitle().isEnab())return;
		
		//页面标题
		
		
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("\r\n<table id='pagetitle' class='pagetitle' width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody><tr>");
			
		if(!StringHelper.IsNullOrEmpty(pageTitleTag.getPageTitle().getTitle())){
			String strPageTitle = pageTitleTag.getPageTitle().getTitle()+"&nbsp;";
			if(this.getBaseTag().getPage().isPageNameDisplay()){
				strPageTitle+="["+this.getBaseTag().getPage().getRequest().getRequestURI().replace(this.getBaseTag().getPage().getContextPath()+"/","")+"]";
			}
			
			Button btn = Toolbar.getNewButton(strPageTitle,"toggleToolbar","toggleToolbar","",this.getBaseTag());//sx-tb-up
			btn.setStyle("width:auto;font-weight:bold;margin-left:2px;float:left; border: 0 solid #D2E0F1;");
			btn.setOnClick(PageJsGear.getPageTitleButtonJs(this.getBaseTag().getPage()));
			
			sb.Append("<td style=\"padding-left:3px;\">");
			sb.Append("<img src=\""+this.getBaseTag().getPage().getContextPath()+"/include/images/jmlog.png\" height=\"23\" width=\"30\" style=\"float:left;\">");
			try {
				this.getOut().write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			btn.onRender(this.getOut());
			sb.Reset();
			sb.Append("</td>");	
		}
		
		if(this.getBaseTag().getPage().isWelcome()){
			sb.Append("<td align=\"right\" style=\"padding-right:3px; font-size:12px;font-weight:bold;font-family:  tahoma, arial, verdana, sans-serif;\">");
		
			String msg = "";
			SimpleDateFormat sf = new SimpleDateFormat("HH");
			int hh = Integer.parseInt(sf.format(new Date()));
			if(hh < 11){
				msg = this.getBaseTag().getPage().getUtils().i18n("早上好")+"!&nbsp;~";
			}else if(hh < 13){
				msg = this.getBaseTag().getPage().getUtils().i18n("中午好")+"!&nbsp;~";
			}else if(hh < 17){
				msg = this.getBaseTag().getPage().getUtils().i18n("下午好")+"!&nbsp;~";
			}else {
				msg = this.getBaseTag().getPage().getUtils().i18n("晚上好")+"!&nbsp;~";
			}
			
			String weekString = "";
	        final String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
	        Calendar calendar = Calendar.getInstance();
	        Date date = new Date();
	        calendar.setTime(date);
	        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
	        weekString = dayNames[dayOfWeek - 1];
	        sb.Append("Dear ");
			sb.Append(this.getBaseTag().getPage().getUtils().getChName());
			sb.Append(",");
			sb.Append(msg);
			sb.Append("&nbsp;&nbsp;");
			sb.Append(this.getBaseTag().getPage().getUtils().formatDate(new Date()));
			sb.Append("&nbsp;&nbsp;");
			sb.Append(this.getBaseTag().getPage().getUtils().i18n(weekString));
			sb.Append("</td>");
		}
		sb.Append("</tr></tbody></table>");
		
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//设置页面标题
		if(!StringHelper.IsNullOrEmpty(pageTitleTag.getPageTitle().getTitle())){
			sb.Reset();
			sb.Append("if($('title').length == 0){$('head').append('<title></title>')} \r\n");
			sb.Append("window.document.title='%1$s';",pageTitleTag.getPageTitle().getTitle());
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, sb.toString());
		}
	}
}
